<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="选择部门">
              <j-select-depart
                v-model="queryParam.unitCode"
                :trigger-change="true"
                customReturnField="id"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="年份">
              <a-date-picker
                mode="year"
                placeholder="请选择年度"
                format="YYYY"
                value-format="YYYY"
                v-model="queryParam.reportYear"
                :open="yearPickShow"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="月份">
              <a-month-picker placeholder="请选择月份" v-model="queryParam.reportMonth" format="MM" value-format="MM" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                返回
              </a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="enterpriseUnit" slot-scope="text">
          <j-ellipsis :value="text" :length="30" />
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handlePreview(record)">预览</a>
          <a-divider type="vertical" />
          <a @click="downloadFile(record)">下载</a>
        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import { getAction } from '@/api/manage'
import moment from 'moment'
import { Base64 } from 'js-base64'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  name: 'sjzjgl',
  mixins: [JeecgListMixin, jumpConfigMixin],
  components: {},
  data() {
    return {
      queryParam: {
        reportYear: '',
      },
      yearPickShow: false,
      dataSource: [],
      // 表头
      columns: columns(this),
      url: {
        list: '/sjzxzbfx/sjzcfz/queryZcfzList',
        downloadfile: '/sjzxzbfx/sjzcfz/exportZcfzb',
      },
      isBackButton: false,
    }
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        }
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    handlePreview(record) {
      getAction(`${this.url.downloadfile}`, {
        accYear: record.reportYear,
        accMonth: record.reportMonth,
        unitId: record.unitCode,
      }).then((res) => {
        if (res.code == 0) {
          let fileUrl = `${window._CONFIG['onlinePreviewFileURL']}/sys/common/download${res.result}`
          console.log(fileUrl)
          // let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(fileUrl)}`
          let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(Base64.encode(fileUrl))}`
          window.open(url, '_blank')
        }
      })
      // return url
    },
    downloadFile(record) {
      getAction(this.url.downloadfile, {
        accYear: record.reportYear,
        accMonth: record.reportMonth,
        unitId: record.unitCode,
      })
        .then((res) => {
          if (res.success && res.result) {
            this.$message.success('正在下载，请稍等！')
            let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
            window.open(url, '_blank')
          } else {
          }
        })
        .finally(() => {})
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
    },
    moment,
    // 得到年份选择器的值
    handlePanelChange(value) {
      this.yearPickShow = false
      this.queryParam.reportYear = moment(value).format('YYYY')
    },
  },
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>